package com.qingjiang.projectschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingjiang.projectschedule.mapper.NodeDelayMapper;
import com.qingjiang.projectschedule.pojo.NodeDelay;
import com.qingjiang.projectschedule.pojo.NodeOperation;
import com.qingjiang.projectschedule.pojo.ProjectInformation;
import com.qingjiang.projectschedule.service.*;
import com.qingjiang.projectschedule.utils.DateUtil;
import com.qingjiang.projectschedule.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jx
 * @description 针对表【node_delay(节点延期表)】的数据库操作Service实现
 * @createDate 2024-03-13 20:05:11
 */
@Service
@Transactional
public class NodeDelayServiceImpl extends ServiceImpl<NodeDelayMapper, NodeDelay>
        implements NodeDelayService {
    @Autowired
    private NodeDelayMapper delayMapper;

    @Autowired
    private ProjectNodeService nodeService;

    @Autowired
    private ProjectInformationService informationService;

    @Autowired
    private ProjectScheduleService scheduleService;

    @Autowired
    private DepartmentService departmentService;

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Result getDelayList(Integer page, Integer pageSize) {
        Page<NodeDelay> nodeDelayIPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<NodeDelay> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NodeDelay::getApproveStatus, 0);
        Page pageResult = delayMapper.selectPage(nodeDelayIPage, queryWrapper);
        List<NodeDelay> delayList = pageResult.getRecords();
        for (NodeDelay nodeDelay : delayList) {
            Integer nodeId = nodeDelay.getNodeId();
            nodeDelay.setNodeName(nodeService.getNodeNameById(nodeId));
            String projectId = nodeDelay.getProjectId();
            ProjectInformation projectInformation = informationService.getById(projectId);
            nodeDelay.setProjectName(projectInformation.getProjectName());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageResult.getTotal());
        map.put("list", delayList);
        return Result.ok(map);
    }

    /**
     * 同意延期申请
     *
     * @param nodeDelay
     * @return
     */
    @Override
    public Result agreeApply(NodeDelay nodeDelay) {
        String projectId = nodeDelay.getProjectId();
        Integer nodeId = nodeDelay.getNodeId();
        LambdaUpdateWrapper<NodeDelay> nodeDelayLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        nodeDelayLambdaUpdateWrapper.eq(NodeDelay::getProjectId, nodeDelay.getProjectId()).eq(NodeDelay::getNodeId, nodeDelay.getNodeId())
                .set(NodeDelay::getApproveMan, nodeDelay.getApproveMan())
                .set(NodeDelay::getApproveComment, nodeDelay.getApproveComment())
                .set(NodeDelay::getApproveTime, dateFormat.format(new Date()))
                .set(NodeDelay::getApproveStatus, 1);
        delayMapper.update(null, nodeDelayLambdaUpdateWrapper);

        scheduleService.updateDelayToTimeById(projectId, nodeId, nodeDelay.getDelayTotime());
        return Result.ok(null);
    }

    @Override
    public Result disagreeApply(NodeDelay nodeDelay) {
        LambdaUpdateWrapper<NodeDelay> nodeDelayLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        nodeDelayLambdaUpdateWrapper.eq(NodeDelay::getProjectId, nodeDelay.getProjectId()).eq(NodeDelay::getNodeId, nodeDelay.getNodeId())
                .set(NodeDelay::getApproveMan, nodeDelay.getApproveMan())
                .set(NodeDelay::getApproveComment, nodeDelay.getApproveComment())
                .set(NodeDelay::getApproveTime, dateFormat.format(new Date()))
                .set(NodeDelay::getApproveStatus, 2);
        delayMapper.update(null, nodeDelayLambdaUpdateWrapper);
        return Result.ok(null);
    }

    @Override
    public Result getApplyResult(Integer uid, Integer page, Integer pageSize) {
        Page<NodeDelay> nodeDelayPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<NodeDelay> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NodeDelay::getApplyManId, uid);
        Page<NodeDelay> pageResult = delayMapper.selectPage(nodeDelayPage, queryWrapper);
        List<NodeDelay> delayList = pageResult.getRecords();
        for (NodeDelay nodeDelay : delayList) {
            Integer nodeId = nodeDelay.getNodeId();
            nodeDelay.setNodeName(nodeService.getNodeNameById(nodeId));
            String projectId = nodeDelay.getProjectId();
            ProjectInformation projectInformation = informationService.getById(projectId);
            nodeDelay.setProjectName(projectInformation.getProjectName());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageResult.getTotal());
        map.put("list", delayList);
        return Result.ok(map);
    }

    @Override
    public boolean postponeApply(NodeOperation nodeOperation) {
        LambdaQueryWrapper<NodeDelay> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NodeDelay::getProjectId, nodeOperation.getProjectId()).eq(NodeDelay::getNodeId,
                nodeOperation.getNodeId());
        List<NodeDelay> result = delayMapper.selectList(queryWrapper);
        String deptName = departmentService.getDeptNameById(nodeOperation.getDeptId());

        if (result.isEmpty()) {
            NodeDelay nodeDelay = new NodeDelay();
            nodeDelay.setProjectId(nodeOperation.getProjectId());
            nodeDelay.setNodeId(nodeOperation.getNodeId());
            nodeDelay.setApplyMan(nodeOperation.getApplyMan());
            LocalDate today = LocalDate.now();
            nodeDelay.setApplyTime(DateUtil.localDate2Date(today));
            nodeDelay.setDelayTotime(DateUtil.string2Date(nodeOperation.getDelayToTime()));
            nodeDelay.setApplyReason(nodeOperation.getApplyReason());
            nodeDelay.setApproveStatus(0);
            nodeDelay.setDeptId(nodeOperation.getDeptId());
            nodeDelay.setDeptName(deptName);
            nodeDelay.setApplyManId(nodeOperation.getApplyManID());
            Integer rows = delayMapper.insert(nodeDelay);
            return rows == 1;
        }
        LambdaUpdateWrapper<NodeDelay> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NodeDelay::getProjectId, nodeOperation.getProjectId())
                .eq(NodeDelay::getNodeId, nodeOperation.getNodeId())
                .set(NodeDelay::getApplyMan, nodeOperation.getApplyMan())
                .set(NodeDelay::getApplyTime, DateUtil.localDate2Date(LocalDate.now()))
                .set(NodeDelay::getDelayTotime, DateUtil.string2Date(nodeOperation.getDelayToTime()))
                .set(NodeDelay::getApplyReason, nodeOperation.getApplyReason())
                .set(NodeDelay::getApproveStatus, 0)
                .set(NodeDelay::getApproveComment, null)
                .set(NodeDelay::getApproveMan, null)
                .set(NodeDelay::getApproveTime, null);
        Integer rows = delayMapper.update(null, updateWrapper);
        return rows == 1;
    }
}




